import pandas as pd
import numpy as np
import statsmodels.api as sm
from sklearn.model_selection import train_test_split
from patsy import dmatrices
# 读取数据集
cardata = pd.read_csv('MTPLdata.csv')
# 数据预处理
cardata['clm'] = cardata['clm'].map(str)
cardata['gas'] = cardata['gas'].map(str)
# 划分训练集和测试集
traindata, testdata = train_test_split(cardata, test_size=0.3, stratify=cardata['clm'], random_state=0)
# 构建设计矩阵
formula = 'clm ~ age + ac + power + gas + brand + area + dens + ct'
y_train, X_train = dmatrices(formula, data=traindata, return_type='dataframe')
y_train = y_train.iloc[:, 1]
y_test, X_test = dmatrices(formula, data=testdata, return_type='dataframe')
y_test = y_test.iloc[:, 1]
# 构建逻辑回归模型
model = sm.Logit(y_train, X_train)
results = model.fit()
# 打印模型摘要
print(results.summary())
# 计算训练集上的性能指标
train_predictions = results.predict(X_train)
train_predictions_binary = (train_predictions >= 0.5).astype(int)
train_accuracy = np.mean(train_predictions_binary == y_train)
train_error_rate = 1 - train_accuracy
train_sensitivity = np.sum((train_predictions_binary == 1) & (y_train == 1)) / np.sum(y_train == 1)
train_specificity = np.sum((train_predictions_binary == 0) & (y_train == 0)) / np.sum(y_train == 0)
train_recall = train_sensitivity
print('Training Error:')
print('Accuracy:', train_accuracy)
print('Error Rate:', train_error_rate)
print('Sensitivity:', train_sensitivity)
print('Specificity:', train_specificity)
print('Recall:', train_recall)
# 计算测试集上的性能指标
test_predictions = results.predict(X_test)
test_predictions_binary = (test_predictions >= 0.5).astype(int)
test_accuracy = np.mean(test_predictions_binary == y_test)
test_error_rate = 1 - test_accuracy
test_sensitivity = np.sum((test_predictions_binary == 1) & (y_test == 1)) / np.sum(y_test == 1)
test_specificity = np.sum((test_predictions_binary == 0) & (y_test == 0)) / np.sum(y_test == 0)
test_recall = test_sensitivity
print('Test Error:')
print('Accuracy:', test_accuracy)
print('Error Rate:', test_error_rate)
print('Sensitivity:', test_sensitivity)
print('Specificity:', test_specificity)
print('Recall:', test_recall)